package cn.stylefeng.guns.modular.system.controller;

import cn.afterturn.easypoi.entity.vo.NormalExcelConstants;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.stylefeng.guns.modular.system.entity.CollectorsAssociation;
import cn.stylefeng.guns.modular.system.enums.ResultEnum;
import cn.stylefeng.guns.modular.system.service.CollectorsAssociationService;
import cn.stylefeng.guns.modular.system.utils.EasyPoiUtil;
import cn.stylefeng.guns.modular.system.utils.ResultVOUtil;
import cn.stylefeng.guns.modular.system.vo.ResultVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.poi.hpsf.Date;
import org.apache.poi.ss.formula.functions.Finance;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName CollectorsAssociationController
 * @Author lzy
 * @Date 2020/2/27 22:17
 * @VERSION 1.0
 */
@Controller
@RequestMapping("/collectorsAssociation")
public class CollectorsAssociationController {
    private final static Logger log = LoggerFactory.getLogger(CollectorsAssociationController.class);
    private static final String SECCESS = "SECCESS";


    private String PREFIX = "/modular/system/association/";

    @Autowired
    private CollectorsAssociationService collectorsAssociationService;

    /**
     * 跳转到主页面
     *
     * @author lzy
     * @Date 2019-06-20
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "collectorsAssociation.html";
    }

    /**
     * 跳转导入excel页面
     *
     * @author lzy
     * @Date 2019-06-20
     */
    @RequestMapping("/importExcel")
    public String importExcel() {
        return PREFIX + "caImportExcel.html";
    }

    @RequestMapping("/selectAssociationList")
    @ResponseBody
    public ResultVO selectAssociationList(@RequestParam(value = "joinStartTime", required = false) String joinStartTime,
                                          @RequestParam(value = "joinEndTime", required = false)String joinEndTime,
                                          @RequestParam(value = "province", required = false)String province,
                                          @RequestParam(value = "city", required = false)String city,
                                          @RequestParam(value = "memberName", required = false)String memberName,
                                          @RequestParam(value = "contactAddress", required = false)String contactAddress,
                                          @RequestParam(value = "collectionRange", required = false)String collectionRange,
                                          @RequestParam(value = "renewalPaymentStartTime", required = false)String renewalPaymentStartTime,
                                          @RequestParam(value = "renewalPaymentEndTime", required = false)String renewalPaymentEndTime,
                                          @RequestParam(value = "mobileNumber", required = false)String mobileNumber,
                                          @RequestParam(value = "startAge", required = false)String startAge,
                                          @RequestParam(value = "endAge", required = false)String endAge,
                                          @RequestParam(value = "sex", required = false)String sex,
                                          @RequestParam("page")Integer pageNum, @RequestParam("limit") Integer pageSize){

        Assert.isTrue(!ObjectUtils.isEmpty(pageNum),ResultEnum.PLEASE_SET_PAGINATION_PARAMETERS.getMessage());
        if (!ObjectUtils.isEmpty(province) && province.contains("省")) {
            province = province.replace("省","");
        }
        //设置默认分页参数
        PageHelper.startPage(pageNum,pageSize);
        //没有市
        List<CollectorsAssociation> list = collectorsAssociationService.findAssociation(joinStartTime,joinEndTime,province,
                                                                                            memberName,contactAddress,
                                                                                            collectionRange,renewalPaymentStartTime,
                                                                                            renewalPaymentEndTime, mobileNumber,
                                                                                            startAge,endAge,sex);
        if (!ObjectUtils.isEmpty(city) && city.contains("市")) {
            //如果有市，过滤
            city = city.replace("市","");
            String finalCity = city;
            list = list.stream().filter(l->!l.getContactAddress().contains(finalCity)).collect(Collectors.toList());
        }
        PageInfo<CollectorsAssociation> pageInfo = new PageInfo<>(list);
        List<CollectorsAssociation> collectorsAssociations = pageInfo.getList();

        return ResultVOUtil.success(collectorsAssociations,pageInfo.getTotal());
    }



    @RequestMapping(value = "/importExcelForType", method = RequestMethod.POST)
    @ResponseBody
    public ResultVO importExcel(@RequestParam("file") MultipartFile file) {

        //上传的Excel进行数据模型解析封装 ，四个参数分别为，上传的文件，excel表标题行数，头行数，实体类class
        List<CollectorsAssociation> tStudents = EasyPoiUtil.importExcel(file, 1, 1, CollectorsAssociation.class);

//        collectorsAssociationService.upload(tStudents);


        return ResultVOUtil.success("成功");
    }



}
